layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "H+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    $.ajax({
        type: "GET",
        headers: {
            token :  $.cookie('token')
        },
        data:{pageNum:1,pageSize:10},
        url: api+"loms/mg/category/all",
        success: function(result){
            tableData = result.data;
            var tableIns = table.render({
                elem: '#d-list',
                data : tableData,
                cellMinWidth : 95,
                page : true,
                height : "300px",
                limit : 20,
                limits : [10,15,20,25],
                id : "d-list",
                cols : [[
                    {field: 'name', title: '分类名称', align:'center'},
                    {field: 'iconUrl', title: '预览图url', align:'center', templet:function (row) {
                        return "<img src='"+row.iconUrl+"'/>";
                        }},
                    {field: 'description', title: '描述', align:'center'},
                    {title: '操作', width:170, templet:'#operator',fixed:"right",align:"center"}
                ]]
            });
        }

    });


    table.on('tool(d-list)', function(obj){
        var layEvent = obj.event,
            rowData = obj.data;
        if(layEvent === 'edit'){ //编辑
            editNews(rowData);
        }else if(layEvent === 'delete'){
            deleteRow(rowData);
        }
    });

    function editNews(row){
        var index = layui.layer.open({
            title : "修改分类",
            type : 2,
            content : "product-edit.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(row){
                    body.find("#id").val(row.id);
                    body.find(".name").val(row.name);
                    body.find(".description").val(row.description);
                    body.find(".iconUrl").val(row.iconUrl);
                    body.find(".iconUrlDisplay").attr('src',row.iconUrl);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    };
    function deleteRow(row) {
        var id = row.id;
        $.ajax({
            type: "POST",
            headers: {
                token :  $.cookie('token')
            },
            url: api+"loms/mg/category/remove/" + id,
            success: function(result){
                console.log(result);
                if(result.code == 1){
                    top.layer.msg("删除成功！");
                    setTimeout(function(){
                        location.reload();
                    },1000);
                } else {
                    top.layer.msg(result.message);
                }
            }
        });
    }
});